const http = require('http');
const fs = require('fs');

const hostname = '127.0.0.1';
const port = 5500;

const server = http.createServer((req, res) => {
    console.log(req.url);

    if (req.url === '/') {
        req.url = '/index.html'; // 如果访问根路径，指向一个默认文件如index.html
    }

    fs.readFile(__dirname + req.url, (err, data) => {
        if (err) {
            res.statusCode = 404;
            res.end(`File not found: ${__dirname + req.url}`);
        } else {
            res.statusCode = 200;
            res.setHeader('Content-Type', 'text/html');
            res.end(data);
        }
    });
});

server.listen(port, hostname, () => {
    console.log(`Server running at http://${hostname}:${port}/`);
});
